package com.enjoyor.soa.traffic.frame.handler.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;

import com.enjoyor.soa.traffic.frame.resolver.ResultErrorResoler;
import com.enjoyor.soa.traffic.util.exception.ManagerException;
import com.enjoyor.soa.traffic.util.exception.RestException;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@ControllerAdvice
public class WebApiExceptionHandler {

    // @Autowired
    // private RestErrorResolver restErrorResolver;
    //
    // @ExceptionHandler({ RestException.class })
    // @ResponseBody
    // public RestError RestExceptionHandler(HttpServletRequest request,
    // HttpServletResponse response, Object handler,
    // Exception ex) {
    // ServletWebRequest webRequest = new ServletWebRequest(request, response);
    // RestError error = this.restErrorResolver.resolveError(webRequest,
    // handler, ex);
    // return error;
    // }
    //
    // @ExceptionHandler({ ManagerException.class })
    // @ResponseBody
    // public RestError ManagerExceptionHandler(HttpServletRequest request,
    // HttpServletResponse response, Object handler,
    // Exception ex) {
    // ServletWebRequest webRequest = new ServletWebRequest(request, response);
    // RestError error = this.restErrorResolver.resolveError(webRequest,
    // handler, ex);
    // return error;
    // }
    //
    // @ExceptionHandler({ Exception.class })
    // @ResponseBody
    // public RestError DefaultExceptionHandler(HttpServletRequest request,
    // HttpServletResponse response, Object handler,
    // Exception ex) {
    // ServletWebRequest webRequest = new ServletWebRequest(request, response);
    // RestError error = this.restErrorResolver.resolveError(webRequest,
    // handler, ex);
    // return error;
    // }

    @Autowired
    private ResultErrorResoler resultErrorResolver;

    @ExceptionHandler({ RestException.class })
    @ResponseBody
    public ResultPojo RestExceptionHandler(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        ServletWebRequest webRequest = new ServletWebRequest(request, response);
        ResultPojo error = this.resultErrorResolver.resolveError(webRequest, handler, ex);
        return error;
    }

    @ExceptionHandler({ ManagerException.class })
    @ResponseBody
    public ResultPojo ManagerExceptionHandler(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        ServletWebRequest webRequest = new ServletWebRequest(request, response);
        ResultPojo error = this.resultErrorResolver.resolveError(webRequest, handler, ex);
        return error;
    }

    @ExceptionHandler({ Exception.class })
    @ResponseBody
    public ResultPojo DefaultExceptionHandler(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        ServletWebRequest webRequest = new ServletWebRequest(request, response);
        ResultPojo error = this.resultErrorResolver.resolveError(webRequest, handler, ex);
        return error;
    }

}
